<?php

namespace Apps\Pay\Controllers;

use Apps\Common\Enum\PayState;
use Apps\Common\Models\UserOrder;
use Apps\Common\Models\UserOrderPayLog;
use Phalcon\Mvc\Controller;

class BaseController extends Controller
{
    /**
     * 支付日志
     * @param int $orderLongId 长订单号
     * @param string $payNumber 支付网关订单号
     * @param int $payType 支付类型
     * @param int $payState 状态
     * @param string|array $logInfo
     */
    protected function savePayLog($orderLongId, $payNumber, $payType, $payState, $logInfo)
    {
        $log = new UserOrderPayLog();
        $log->order_id = $orderLongId;
        $log->order_pay_type = $payType;
        $log->order_pay_number = $payNumber;
        $log->order_pay_state = $payState;
        $log->order_pay_log_info = is_string($logInfo) ? $logInfo : json_encode($logInfo);
        if($log->save() == false) {
            $this->databaseErrorLog($log);
        }
    }

    /**
     * 处理订单支付信息
     * @param $echoResult
     * @param $payType
     * @param $transactionId
     * @param $outTradeNo
     * @param $totalFee
     * @param $params
     */
    protected function payHandle($echoResult, $payType,$transactionId, $outTradeNo, $totalFee, $params)
    {
        // 获取订单信息
        $orderInfo = $this->getOrderInfo($outTradeNo);
        if(!$orderInfo) {
            $this->savePayLog($outTradeNo, $transactionId, $payType, PayState::ASYNC_FAIL_ORDER, $params);
            $echoResult(false, 'order not exist');
            return;
        }
        $orderId = $orderInfo->order_id;
        // 核对价格
        if(intval($orderInfo->order_money * 100) != $totalFee) {
            $this->savePayLog($outTradeNo, $transactionId, $payType, PayState::ASYNC_FAIL_MONEY, $params);
            $echoResult(false, 'totalfee != order money');
            return;
        }
        // 设置订单支付成功
        if($this->setOrderPaySuccess($orderInfo, $payType) == false) {
            $this->savePayLog($outTradeNo, $transactionId, $payType, PayState::DATABASE_FAIL, $params);
            $echoResult(false, 'database error');
            return;
        }
        // 保存日志
        $this->savePayLog($outTradeNo, $transactionId, $payType, PayState::ASYNC_SUCCESS, $params);
        $echoResult(true);
    }

    /**
     * @param $orderLongId
     * @return UserOrder
     */
    protected function getOrderInfo($orderLongId)
    {
        return UserOrder::findFirst('order_long_id = ' . $orderLongId);
    }

    /**
     * 设置订单支付成功
     * @param \Apps\Common\Models\UserOrder $orderInfo
     * @param int $payType
     * @return bool
     */
    protected function setOrderPaySuccess($orderInfo, $payType)
    {
        $data = ['order_pay_type' => $payType, 'order_pay_state' => 2];
        if($orderInfo->order_state < 2) {
            $data['order_state'] = 2;
        }
        return $orderInfo->update($data);
    }

    /**
     * 数据库操作异常
     * @param \Phalcon\Mvc\Model $model
     * @return bool
     */
    protected function databaseErrorLog($model)
    {
        $error = '';
        foreach($model->getMessages() as $msg)
        {
            $error .= $msg . ',';
        }
        $this->di->get('logger')->error('数据库异常, GET:' . json_encode($_GET) . ', POST:' . json_encode($_POST) . ', error: ' . $error);
        return false;
    }
}